package com.wst;

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

public class GenerateKey {

    public static void main(String[] args) throws Exception {
        KeyGenerator keyGen = KeyGenerator.getInstance("AES");
        keyGen.init(256); //使用 128, 192, 或 256 位的 AES 密钥
        SecretKey secretKey = keyGen.generateKey();
        byte[] keyBytes = secretKey.getEncoded();

        // 将密钥转换为十六进制字符串
        StringBuilder sb = new StringBuilder();
        for (byte b : keyBytes) {
            sb.append(String.format("%02x", b));
        }
        System.out.println("Generated AES Key: " + sb.toString());
    }
}
